<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet">
  <style>
    [v-cloak]{
      display: none;
    }
  </style>
</head>
<body>
  <div id="app">
    <div id="header" style="max-width:600px;margin-top:30px" class="center-block" >
      <h2>今日事、今日毕</h2>
      <div class="row">
        <div class="col-md-9 col-sm-9">
          <input type="text" v-model.trim="inputTxt" class="form-control">
        </div>
        <div class="col-md-3 col-sm-3">
          <button @click="addTodo" class="btn btn-success">增加事项</button>
        </div>
      </div>
    </div>
    <div id="body" class="center-block" style="max-width:600px;margin-top:30px">
      <ul class="list-group" v-cloak>
        <li class="list-group-item text-center" v-if="!todos.length">
          暂无待办事项，请添加
        </li>
        <li 
          class="list-group-item clearfix"
          v-for="(todo,index) in todos"
          :key="index"
        >
          <p class="pull-left">{{todo.content}}</p>
          <div class="pull-right">
            <button 
              :disabled="todo.isCompleted" 
              @click="setState(index)"
              class="btn btn-primary btn-xs">{{todo.isCompleted?'已完成':'点击完成'}}</button>
            <button 
              @click="delTodo(index)"
              class="btn btn-danger btn-xs">删除</button>
          </div>
        </li>
      </ul>
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el:"#app",
      data:{
        inputTxt:'',
        todos: [
        ]
      },
      methods: {
        addTodo(){
          // 添加待办事项
          if(!this.inputTxt){
            alert('请输入代办事项');
            return;
          }
          this.todos.unshift({
            content: this.inputTxt,
            isCompleted: false
          })
          this.inputTxt=""
        },
        delTodo(index){
          // 删除
          if(confirm('您确认删除吗？')){
            this.todos.splice(index, 1)
          }
        },
        setState(index){
          this.todos[index].isCompleted = true
          console.log(this.todos)
        }
      }
    })

    arr = [1,2,3,4,5]
    //  filter map find findIndex some every forEach arr.splice(index,1)
  </script>
</body>
</html>
